package analyser;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.swing.JFrame;
import pojo.Protocol;
import processor.SwitchWin;
import processor.UpdateChatWin;
import processor.UpdateLoginWin;
import ui.WindowTypes;

public class ProtocolAnalyser {
	private Map<Integer, JFrame> windows;
	public ProtocolAnalyser(Map<Integer, JFrame> windows) {
		super();
		this.windows = windows;
	}
	
	public void analyse(Protocol protocol){
		JFrame loginWin = windows.get(WindowTypes.LOGIN_WIN);
		JFrame chatWin = windows.get(WindowTypes.CHAT_WIN);
		JFrame registWin = windows.get(WindowTypes.REGIST_WIN);
		UpdateLoginWin updateLoginWin = new UpdateLoginWin(loginWin);
		UpdateChatWin updateChatWin = new UpdateChatWin(chatWin);
		String msg, from, to;
		Date time;
		
		switch (protocol.getType()) {
		case Protocol.LOGOUT:
			updateChatWin.quit();
			break;
		case Protocol.PRIVATE_MSG:
		case Protocol.GROUP_MSG:
			msg = protocol.getMsg();
			from = protocol.getFrom();
			to = protocol.getTo();
			time = protocol.getTime();
			updateChatWin.updateChatMsg(msg, from,to, time);
			break;
		case Protocol.PRIVATE_SHAKE:
		case Protocol.GROUP_SHAKE:
			from = protocol.getFrom();
			time = protocol.getTime();
			to = protocol.getTo();
			updateChatWin.shakeWindow(from, to, time);
			break;
		case Protocol.ERROR:
			loginWin = windows.get(WindowTypes.LOGIN_WIN);
			String errorMsg = protocol.getMsg();
			updateLoginWin.error(errorMsg);
			break;
		case Protocol.LOGIN_SUCCEED:
			List<String> list = protocol.getOnlineList();
			to = protocol.getTo();
			new SwitchWin(chatWin, loginWin).proceed(to);
			updateChatWin.addUserList(list);
			break;
		case Protocol.REGISTER_SUCCESS:
			List<String> list2 = protocol.getOnlineList();
			to = protocol.getTo();
			new SwitchWin(chatWin, registWin).proceed(to);
			updateChatWin.addUserList(list2);
			break;
		case Protocol.SOMEONE_LOGIN:
			from = protocol.getFrom();
			time = protocol.getTime();
			updateChatWin.addUser(from, time);
			break;
		case Protocol.SOMEONE_LOGOUT:
			from = protocol.getFrom();
			time = protocol.getTime();
			updateChatWin.removeUser(from, time);
			break;
		default:
			break;
		}
	}

}
